package com.xinzhi.savingpot.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 第三方接口 Util 类 ---- 刘
 */
public class IdentificationUtil {
    static String appCode = "01fa85f264fc4c04bece311fa05de17d";

    /**
     *  验证身份证正确性
     * @param name   名字
     * @param identityCard   身份证号
     * @return
     */
    public static Map<String , Object> verifyIdentityCard(String name,String identityCard){
        Map<String, Object> map = new HashMap<>();
        String host = "https://lfeid.market.alicloudapi.com";
        String path = "/idcheck/life";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("cardNo", identityCard);
        querys.put("realName", name);
        OkHttpClient client = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(host+path+buildRequestUrl(querys))
                .addHeader("Authorization", "APPCODE " + appCode)
                .build();
        String result = null;
        try {
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        map.put("code",jsonObject.getString("error_code"));
        map.put("msg",jsonObject.getString("reason"));
        map.put("isok",JSONObject.parseObject(jsonObject.getString("result")).get("isok"));
        return map;
    }

    /**
     * 身份证照片提取
     *
     * @param urls  图片的url
     * @param PAndN 图片是正反（1正面，2反面）
     * @return map集合（PAndN 为1  code状态码 200成功  msg返回信息  number 身份证号 name 姓名
     * PAndN 为2  code状态码 200成功 msg返回信息    end_date到期时间   issue 签发机关   start_date 开始时间）
     * @throws IOException
     */
    public static Map<String, Object> identityCard(String urls, String PAndN) {
        Map<String, Object> map = new HashMap<>();
        String host = "https://api05.aliyun.venuscn.com";
        String path = "/ocr/id-card";
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("pic", urls);
        builder.add("type", PAndN);
        FormBody body = builder.build();
        Request request = new Request.Builder().url(host + path).addHeader("Authorization", "APPCODE " + appCode)
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .post(body)
                .build();
        JSONObject jsonObject = null;
        try {
            Response response = client.newCall(request).execute();
            jsonObject = JSONObject.parseObject(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data = jsonObject.getString("data");
        map.put("code", jsonObject.getString("ret"));
        map.put("msg", jsonObject.getString("msg"));
        map.put("data", JSONObject.parseObject(data));
        System.out.println(map);
        return map;
    }

    public static String buildRequestUrl(Map<String, String> params) {
        StringBuilder url = new StringBuilder("?");
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            url.append(key).append("=").append(params.get(key)).append("&");
        }
        return url.toString().substring(0, url.length() - 1);
    }
}
